<?php
/**
 *
 * This file contains {@link Domain51_Service_Delicious_Posts}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */


/**
 *
 * Required interface for connection
 *
 */
Solar::loadClass('Domain51_Util_Service_Connection');


/**
 *
 * The object that handles formatting requests for tags/bundles API calls to 
 * del.icio.us
 *
 * This class should not be instantiated directly when used during normal use,
 * instead it should be referenced via {@link Domain51_Service_Delicious_Tags}.
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 * @see Domain51_Service_Delicious_Tags
 *
 */
class Domain51_Service_Delicious_Tags_Bundles
{
    private $_connection = null;
    
    /**
     *
     * Handle instantiation
     *
     *
     * @param Domain51_Util_Service_Connection $connection
     *
     * @see Domain51_Service_Delicious
     *
     */
    public function __construct(Domain51_Util_Service_Connection $connection) {
        $this->_connection = $connection;
    }
    
    
    /**
     *
     * Retrieve all of the user's bundles by performing a 
     * <code>tags/bundles/all</code> API call.
     *
     * <b>NOTE</b>: I was unable to get this to work, thus the todo mentioned
     * below.  This is a documented method listed on the 
     * {@link http://del.icio.us/help/api/bundles del.icio.us bundles API page}.
     *
     * Except the return of this method to change once this API call has been
     * fixed.
     *
     * @todo Fix by returning a straight array once del.icio.us starts working
     */
    public function all() {
        $uri = 'tags/bundles/all';
        $responseXml = $this->_connection->doRequest($uri);
        return $responseXml;
    }
    
    
    /**
     *
     * Assign a set of tags to to a single bundle by performing a
     * <code>tags/bundles/set</code> API call.
     *
     * This clear away any previous set bundles.
     *
     *
     * @param string $bundle Bundle name
     *
     * @param string $tags A list of space-separate tags
     *
     * @return bool
     *
     */
    public function set($bundle, $tags) {
        $params = array(
            'bundle=' . urlencode($bundle),
            'tags=' . urlencode($tags)
        );
        $uri = 'tags/bundles/set?' . implode('&', $params);
        $responseXml = $this->_connection->doRequest($uri);
        return ((string)$responseXml == 'ok');
    }
    
    
    /**
     *
     * Delete a bundle by performing a <code>tags/bundle/delete</code> API call.
     *
     * Note that this deviates from the 
     * {@link http://del.icio.us/help/api/bundles spec} in that it checks for
     * "ok" on return while the sepc says "done" will be returned.  In 
     * testing this code, "ok" was the actual return despite documentation 
     * stating otherwise.
     *
     *
     * @param string $bundle Name of the bundle to remove
     *
     * @return bool
     *
     */
    public function delete($bundle) {
        $params = 'bundle=' . urlencode($bundle);
        $uri = 'tags/bundles/delete?' . $params;
        $responseXml = $this->_connection->doRequest($uri);
        return ((string)$responseXml == 'ok');
    }
}